﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MobileWebsiteGenerator.Models;
using System.Threading;

namespace MobileWebsiteGenerator
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static Boolean IsHebrew = false;
        public static bool IsUpdateMode = false;
        public static bool IsLoggedIn = false;
        public static bool IsLoggedInFront = false;
        public static bool IscreateNew = false;
        public static bool IscreateNewFront = false;
        public static int? CustomerID;

        public static int pageId = 4;

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static MainViewModel _MainCustomer = new MainViewModel();
        public static bool showMessage = false;
        public static string messageTxt = string.Empty;
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            _MainCustomer.customer = new Entities.Customer();
            _MainCustomer.ContentPages = new List<Entities.Page>();
            _MainCustomer.MyTheme = new Entities.ThemeDetail();
            _MainCustomer.PictureGallery = new List<Entities.pictureGallery>();
            _MainCustomer.Website = new Entities.Website();
            _MainCustomer.metatags = new Entities.Metatag();
            Application["OnlineUsers"] = 0;

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            Application.Lock();
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
            Application.UnLock();
        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
            Application.Lock();
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
            Application.UnLock();
        }






        public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            // Check for culture type of caching
            if (custom == "culture")

            {

                // culture name (e.g. "en-US") is what should vary caching
                return Thread.CurrentThread.CurrentCulture.Name;

            }

            else

                return base.GetVaryByCustomString(context, custom);
        }
    }
}